home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / text / dtp / t1utils.lha / t1utils / t1ascii.c < prev    next >
C/C++ Source or Header  |  1993-06-17  |  3KB  |  154 lines

  1. /* t1ascii
  2. **
  3. ** This program takes an Adobe Type-1 font program in binary (PFB) format and
  4. ** converts it to ASCII (PFA) format.
  5. **
  6. ** Copyright (c) 1992 by I. Lee Hetherington, all rights reserved.
  7. **
  8. ** Permission is hereby granted to use, modify, and distribute this program
  9. ** for any purpose provided this copyright notice and the one below remain
  10. ** intact. 
  11. **
  12. ** author: I. Lee Hetherington (ilh@lcs.mit.edu)
  13. */
  14.  
  15. #ifndef lint
  16. static char sccsid[] =
  17.   "@(#) t1ascii.c 1.1 10:58:15 5/21/92";
  18. static char copyright[] =
  19.   "@(#) Copyright (c) 1992 by I. Lee Hetherington, all rights reserved.";
  20. #endif
  21.  
  22. /* Note: this is ANSI C. */
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28.  
  29. #ifdef MSDOS
  30. #define RB "rb"
  31. #else
  32. #define RB "r"
  33. #endif
  34.  
  35. #define BANNER   "This is t1ascii 1.1.\n"
  36.  
  37. #define MARKER   128
  38. #define ASCII    1
  39. #define BINARY   2
  40. #define DONE     3
  41.  
  42. static FILE *ifp = stdin;
  43. static FILE *ofp = stdout;
  44.  
  45. /* This function reads a four-byte block length. */
  46.  
  47. static int read_length()
  48. {
  49.   int length;
  50.  
  51.   length = fgetc(ifp);
  52.   length |= fgetc(ifp) << 8;
  53.   length |= fgetc(ifp) << 16;
  54.   length |= fgetc(ifp) << 24;
  55.  
  56.   return length;
  57. }
  58.  
  59. /* This function outputs a single byte in hexadecimal.  It limits hexadecimal
  60.    output to 64 columns. */
  61.  
  62. static void output_hex(int b)
  63. {
  64.   static char *hexchar = "0123456789ABCDEF";
  65.   static int hexcol = 0;
  66.  
  67.   /* trim hexadecimal lines to 64 columns */
  68.   if (hexcol >= 64) {
  69.     fputc('\n', ofp);
  70.     hexcol = 0;
  71.   }
  72.   fputc(hexchar[(b >> 4) & 0xf], ofp);
  73.   fputc(hexchar[b & 0xf], ofp);
  74.   hexcol += 2;
  75. }
  76.  
  77. static void usage()
  78. {
  79.   fprintf(stderr,
  80.       "usage: t1ascii [input [output]]\n");
  81.   exit(1);
  82. }
  83.  
  84. int main(int argc, char **argv)
  85. {
  86.   int c, block = 1, length, last_type = ASCII;
  87.  
  88.   fprintf(stderr, "%s", BANNER);
  89.  
  90.   if (argc > 3)
  91.     usage();
  92.  
  93.   /* possibly open input & output files */
  94.   if (argc >= 2) {
  95.     ifp = fopen(argv[1], RB);
  96.     if (!ifp) {
  97.       fprintf(stderr, "error: cannot open %s for reading\n", argv[1]);
  98.       exit(1);
  99.     }
  100.   }
  101.   if (argc == 3) {
  102.     ofp = fopen(argv[2], "w");
  103.     if (!ofp) {
  104.       fprintf(stderr, "error: cannot open %s for writing\n", argv[2]);
  105.       exit(1);
  106.     }
  107.   }
  108.  
  109.   /* main loop through blocks */
  110.  
  111.   for (;;) {
  112.     c = fgetc(ifp);
  113.     if (c == EOF) {
  114.       break;
  115.     }
  116.     if (c != MARKER) {
  117.       fprintf(stderr,
  118.           "error:  missing marker (128) at beginning of block %d",
  119.           block);
  120.       exit(1);
  121.     }
  122.     switch (c = fgetc(ifp)) {
  123.     case ASCII:
  124.       if (last_type != ASCII)
  125.     fputc('\n', ofp);
  126.       last_type = ASCII;
  127.       for (length = read_length(); length > 0; length--)
  128.     if ((c = fgetc(ifp)) == '\r')
  129.       fputc('\n', ofp);
  130.     else
  131.       fputc(c, ofp);
  132.       break;
  133.     case BINARY:
  134.       last_type = BINARY;
  135.       for (length = read_length(); length > 0; length--)
  136.     output_hex(fgetc(ifp));      
  137.       break;
  138.     case DONE:
  139.       /* nothing to be done --- will exit at top of loop with EOF */
  140.       break;
  141.     default:
  142.       fprintf(stderr, "error: bad block type %d in block %d\n",
  143.           c, block);
  144.       break;
  145.     }
  146.     block++;
  147.   }
  148.   fclose(ifp);
  149.   fclose(ofp);
  150.  
  151.   return 0;
  152. }
  153.  
  154.